﻿#include "Monkey.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"

using namespace cocos2d;
using namespace cocos2d::extension;



Monkey * Monkey::create(CCNode* monkeyNode)
{
	Monkey *monkey = new Monkey();
    if (monkey && monkey->init(monkeyNode))
    {
        monkey->autorelease();
        return monkey;
    }
    else
    {
        CC_SAFE_DELETE(monkey);
        return NULL;
    }
}
bool Monkey::init(CCNode* monkeyNode)
{
	CCLog("Khoi tao con khi");
	CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo(kImageMonkey, kPlistMonkey, kJsonMonkey);
	mScale = 0.4f;
	mArmature = CCArmature::create(kNameMonkey);
	mArmature->setPosition(0,0);

	mArmature->setScaleX(mScale * MKLEFT); // Set huong quay mat cua con khi
	mArmature->setScaleY(mScale);

    this->addChild(mArmature,500); 

	setStatus(MKIDLE); // Trang thai ban dau cua con khi
	mCurrentStatus = MKIDLE;
    mNewStatus = MKIDLE;

	mCurrentDirection = MKLEFT;
	mNewDirection = MKRIGHT;

	fSetHeath(MKHEATHFULL); // Luong mau ban dau cua con khi
	// Pre load music
	fPreLoadMusic();

	this->schedule(schedule_selector(Monkey::update));
	return true;
}

// Preload tat ca cac music effect
void Monkey::fPreLoadMusic()
{
	CCLog("PreLoad music !!!!");
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}
// Update cac trang thai cua con khi
void Monkey::update(float dt)
{
// Kiem tra huong xoay hien tai cua con khi
	if(mCurrentDirection == mNewDirection)
	{
		return;
	}else{
		mCurrentDirection = mNewDirection;
		fChangeDirection();
	}
		
	if (mCurrentStatus == mNewStatus){
		return;
	}else // cap nhat lai animation
    {
		CCLog("Co thay doi trang thai");
        mCurrentStatus = mNewStatus;
        fEffect();
    }
}
// Xoay mat con khi
void Monkey::fChangeDirection()
{
	if(mCurrentDirection == MKLEFT)
	{
		CCLog("Quay sang phai");
		mArmature->setScaleX(mScale * getDirection());
		mArmature->setScaleY(mScale);
	}else{
		CCLog("Quay sang trai");
		mArmature->setScaleX(mScale * getDirection());
		mArmature->setScaleY(mScale);
	}
	
}
void Monkey::fEffect()
{

	switch(mCurrentStatus)
	{
			//action
			//play sound effect here

		case MKIDLE: //dung yen
			mArmature->getAnimation()->play("mua"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKCLIMBING: //treo
			mArmature->getAnimation()->play("treo"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKFALL: //roi
			mArmature->getAnimation()->play("mua"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKDEATH: //chet
			mArmature->getAnimation()->play("mua");
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKFOOT: // nhay mua
			mArmature->getAnimation()->play("mua"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKFLYTHROUGHTREE: //bay qua cay
			mArmature->getAnimation()->play("nhaybamcay"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKFLYTHROUGHWIRE: // bay qua day
			mArmature->getAnimation()->play("nhayduday"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case MKWEARY: //met moi
			mArmature->getAnimation()->play("mua"); 
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		default: 
			CCLog("Default effect broken !!");
			break;
	}
}


// Amimation callback cua con khi
void Monkey::onAnimationEvent(cocos2d::extension::CCArmature *pArmature, cocos2d::extension::MovementEventType eventType, const char *animationID)
{
	std::string id = animationID;
    if (eventType == COMPLETE)
    {
		CCLog("Animation complete");
		CCLog("Animation ID = %d",animationID);

        if (id.compare("broken") == 0)
        {	
			CCLog("It's broken !!");
			pArmature->getAnimation()->stop();// Stop animation when loop complate
        }
    }

}


void Monkey::fSetPosition(CCPoint point)
{
	CCNode::setPosition(point);
}

void Monkey::fSetPosition(float x, float y)
{
	CCNode::setPosition(x,y);
}

CCPoint Monkey::fGetPosition()
{
	return CCNode::getPosition();
}

CCArmature* Monkey::fGetArmature()
{
	return mArmature;
}